package com.example.sports_meet;


import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.sports_meet.adapter.SelectAdapter;
import com.example.sports_meet.entity.SelectM;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class OngoingeventActivity extends AppCompatActivity {

    private Button btnS;
    private ListView listView;
    private static int b = 0;
    private List<SelectM> books = new ArrayList<SelectM>();
    private MyDbHelper myDbHelper;
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ongoingevent);
        Objects.requireNonNull(getSupportActionBar()).hide();

        myDbHelper = new MyDbHelper(this, "Mydatabase", null, 1);

        btnS = (Button) findViewById(R.id.btns);
        listView = (ListView) findViewById(R.id.listview);
        if (b == 0) {
            db = myDbHelper.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put("eno", "202204001");
            values.put("ename", "男子100米自由泳");
            values.put("etime", "2022-06-13 08:00:00");
            values.put("fno", "202207001");
            values.put("jno", "202203001");
            values.put("sno", "202205001");
            values.put("esex", "男");
            db.insert("eevent", null, values);
            ContentValues values1 = new ContentValues();
            values1.put("eno", "202204002");
            values1.put("ename", "女子100米自由泳");
            values1.put("etime", "2022-06-13 08:30:00");
            values1.put("fno", "202207001");
            values1.put("jno", "202203001");
            values1.put("sno", "202205001");
            values1.put("esex", "女");
            db.insert("eevent", null, values1);
            ContentValues values2 = new ContentValues();
            values2.put("eno", "202204003");
            values2.put("ename", "女子水上芭蕾");
            values2.put("etime", "2022-06-13 10:00:00");
            values2.put("fno", "202207001");
            values2.put("jno", "202203002");
            values2.put("sno", "202205001");
            values2.put("esex", "女");
            db.insert("eevent", null, values2);
            db.close();
            b++;
        }
        btnS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                books.clear();
                SelectM selectMs = new SelectM("项目号", "项目名", "比赛时间");
                books.add(selectMs);

                db = myDbHelper.getReadableDatabase();
                Cursor cursor = db.query("eevent", new String[]{"eno", "ename", "etime"}, "etime=?", new String[]{"2022-06-13 10:00:00"}, null, null, null);
                if (cursor.getCount() == 0) {
                    Toast.makeText(OngoingeventActivity.this, "没有数据", Toast.LENGTH_LONG).show();
                } else {
                    cursor.moveToFirst();
                    String ENO = "eno";
                    for (int i = 0; i < cursor.getCount(); i++) {
                        String eno = cursor.getString(0);
                        String ename = cursor.getString(1);
                        String etime = cursor.getString(2);
                        cursor.moveToNext();
                        SelectM selectM = new SelectM(eno, ename, etime);
                        books.add(selectM);
                    }
                }
                cursor.close();
                db.close();
                SelectAdapter selectAdapter = new SelectAdapter(books, OngoingeventActivity.this);
                listView.setAdapter(selectAdapter);
            }
        });
    }
}